package hhm.android.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import com.google.gson.Gson
import siau.android.base.HttpHelper
import siau.android.base.MyFlutterActivity
import siau.android.base.WEBVIEW_ROUTER
import siau.android.http.model.ActiveIdModel
import siau.android.http.model.CommunityArticleDetailModel
import siau.android.http.model.CommunityTopicDetailModel
import siau.android.http.model.SchemePageModel
import siau.android.library.KeyString

fun schemeDataAnalyse(context: Context, data: String) {
    var pageType = -1
    var pageId = -1
    var pageData = ""
    try {
        val split = data.split("?")
        val stringParams = split[1].split("&")
        stringParams.forEach {
            if (it.startsWith("param")) {
                val paramParams = it.split("=")
                val model = Gson().fromJson(paramParams[1], SchemePageModel::class.java)
                pageType = model.pageType
                pageId = model.pageId
            } else if (it.startsWith("data")) {
                pageData = it.split("=")[1]
            }
        }
        if (pageType == -1 || pageId == -1) {
            Toast.makeText(context, "参数解析错误", Toast.LENGTH_SHORT).show()
            return
        }
        when (pageType) {
            1 -> {
                val r = CommunityArticleDetailModel(pageId, "typeArticle", pageData)
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.ArticleDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            2 -> {
                val r = CommunityArticleDetailModel(pageId, "typeVideo", pageData)
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.VideoDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            3 -> {
                val r = CommunityTopicDetailModel(pageId, pageData)
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.HotTopicDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            4 -> {
                val r = CommunityArticleDetailModel(pageId, "typeTopicArticle", pageData)
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.ArticleDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            5 -> {
                val r = CommunityArticleDetailModel(pageId, "typeTopicVideo", pageData)
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.VideoDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            6 -> {
                val r = ActiveIdModel(pageId, pageData)
                HttpHelper().communityActivityDetail(r)?.subscribe({
                    if (it.activeUrl.contains("?")) {
                        it.activeUrl = it.activeUrl + "&data=" + pageData
                    } else {
                        it.activeUrl = it.activeUrl + "?data=" + pageData
                    }
                    val bundle = Bundle()
                    bundle.putBoolean(KeyString.share, true)
                    bundle.putSerializable(KeyString.data, it)
                    ARouter.getInstance().build(WEBVIEW_ROUTER).with(bundle).navigation()
                }, {
                    Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
                })
            }
        }
    } catch (e: Exception) {
        Toast.makeText(context, "参数解析错误", Toast.LENGTH_SHORT).show()
    }
}

fun linkDataAnalyse(context: Context, data: String) {
    var pageType = -1
    var pageId = -1
    try {
        val split = data.split("?")
        val stringParams = split[1].split("&")
        stringParams.forEach {
            if (it.startsWith("pageType")) {
                val paramParams = it.split("=")
                pageType = paramParams[1].toInt()
            } else if (it.startsWith("pageId")) {
                val paramParams = it.split("=")
                pageId = paramParams[1].toInt()
            }
        }
        if (pageType == -1 || pageId == -1) {
            Toast.makeText(context, "参数解析错误", Toast.LENGTH_SHORT).show()
            return
        }
        when (pageType) {
            1 -> {
                val r = CommunityArticleDetailModel(pageId, "typeArticle", "{}")
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.ArticleDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            2 -> {
                val r = CommunityArticleDetailModel(pageId, "typeVideo", "{}")
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.VideoDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            3 -> {
                val r = CommunityTopicDetailModel(pageId, split[1])
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.HotTopicDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            4 -> {
                val r = CommunityArticleDetailModel(pageId, "typeTopicArticle", "{}")
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.ArticleDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            5 -> {
                val r = CommunityArticleDetailModel(pageId, "typeTopicVideo", "{}")
                val i = Intent(context, MyFlutterActivity::class.java)
                i.putExtra(KeyString.route, KeyString.VideoDetailPage + "&&&" + Gson().toJson(r))
                context.startActivity(i)
            }
            6 -> {
                val r = ActiveIdModel(pageId, split[1])
                HttpHelper().communityActivityDetail(r)?.subscribe({
                    if (it.activeUrl.contains("?")) {
                        it.activeUrl = it.activeUrl + "&data=" + "{}"
                    } else {
                        it.activeUrl = it.activeUrl + "?data=" + "{}"
                    }
                    val bundle = Bundle()
                    bundle.putBoolean(KeyString.share, true)
                    bundle.putSerializable(KeyString.data, it)
                    ARouter.getInstance().build(WEBVIEW_ROUTER).with(bundle).navigation()
                }, {
                    Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
                })
            }
        }
    } catch (e: Exception) {
        Toast.makeText(context, "参数解析错误", Toast.LENGTH_SHORT).show()
    }
}